package com.iflytek.jobs.admin.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.iflytek.jobs.service.domain.JobInfo;
import com.iflytek.jobs.service.domain.vo.JobInfoExcel;
import com.iflytek.jobs.service.service.IJobInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 岗位信息Controller
 *
 * @author chenhs
 * @date 2023-02-03
 */
@RestController
@RequestMapping("/jobs/jobinfo")
public class JobInfoController extends BaseController {
    @Autowired
    private IJobInfoService jobInfoService;


    @Log(title = "导入岗位信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @PreAuthorize("@ss.notRole('demo')")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<JobInfoExcel> util = new ExcelUtil<JobInfoExcel>(JobInfoExcel.class);
        List<JobInfoExcel> jobList = util.importExcel(file.getInputStream());
        jobInfoService.importData(jobList);
        return AjaxResult.success();
    }


    @PostMapping("/importTemplate")
    @PreAuthorize("@ss.notRole('demo')")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<JobInfoExcel> util = new ExcelUtil<JobInfoExcel>(JobInfoExcel.class);
        util.importTemplateExcel(response, "岗位信息表模板");
    }

    /**
     * 查询岗位信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(JobInfo jobInfo) {
        startPage();
        List<JobInfo> list = jobInfoService.selectJobInfoList(jobInfo);
        return getDataTable(list);
    }

    /**
     * 导出岗位信息列表
     */
    @Log(title = "岗位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @PreAuthorize("@ss.notRole('demo')")
    public void export(HttpServletResponse response, JobInfo jobInfo) {
        List<JobInfo> list = jobInfoService.selectJobInfoList(jobInfo);
        ExcelUtil<JobInfo> util = new ExcelUtil<JobInfo>(JobInfo.class);
        util.exportExcel(response, list, "岗位信息数据");
    }

    /**
     * 获取岗位信息详细信息
     */
    @GetMapping(value = "/{jobId}")
    public AjaxResult getInfo(@PathVariable("jobId") String jobId) {
        return success(jobInfoService.selectJobInfoByJobId(jobId));
    }

    /**
     * 新增岗位信息
     */
    @Log(title = "岗位信息", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.notRole('demo')")
    @PostMapping
    public AjaxResult add(@RequestBody JobInfo jobInfo) {
        return toAjax(jobInfoService.insertJobInfo(jobInfo));
    }

    /**
     * 修改岗位信息
     */
    @Log(title = "岗位信息", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.notRole('demo')")
    @PutMapping
    public AjaxResult edit(@RequestBody JobInfo jobInfo) {
        return toAjax(jobInfoService.updateJobInfo(jobInfo));
    }

    /**
     * 删除岗位信息
     */
//    @PreAuthorize("@ss.hasPermi('jobs:INFO:remove')")
    @Log(title = "岗位信息", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.notRole('demo')")
    @DeleteMapping("/{jobIds}")
    public AjaxResult remove(@PathVariable String[] jobIds) {
        return toAjax(jobInfoService.deleteJobInfoByJobIds(jobIds));
    }
}
